<template>
  <el-dialog
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :title="dialogData.title"
    :visible.sync="isDialog"
    width="600px"
  >
    <el-form
      ref="submitFormRef"
      :model="submitForm"
      :rules="submitFormRules"
      label-position="right"
      label-width="auto"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="submitForm.name"
          clearable
          placeholder="请输入名称"
          type="text"
        ></el-input>
      </el-form-item>

      <!--      <el-form-item label="图标" prop="picture_path">-->
      <!--        <el-upload-->
      <!--          :http-request="uploadImage"-->
      <!--          :show-file-list="false"-->
      <!--          action="#"-->
      <!--          list-type="picture-card"-->
      <!--        >-->
      <!--          <el-image-->
      <!--            v-if="submitForm.picture_path"-->
      <!--            :src="submitForm.picture_path"-->
      <!--            class="image-box"-->
      <!--          >-->
      <!--            <div slot="error" class="image-slot">-->
      <!--              <img alt="没有图片" src="@/assets/imgs/error-img.png" />-->
      <!--            </div>-->
      <!--          </el-image>-->
      <!--          <i v-else class="el-icon-plus avatar-uploader-icon"></i>-->
      <!--        </el-upload>-->
      <!--        <div>-->
      <!--          <span class="color-tips1">提示：请上传宽800px高800px的图片</span>-->
      <!--        </div>-->
      <!--      </el-form-item>-->

      <el-form-item label="排序" prop="sort">
        <el-slider v-model="submitForm.sort" :max="255" show-input></el-slider>
        <span class="form-tip">越小越靠前</span>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button
        :loading="isLoading.submit"
        type="primary"
        @click="handleSubmit"
      >
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from "@/api/category";
import { upload as uploadFiles } from "@/api/image";

export default {
  name: "Edit",
  props: {
    info: {
      type: Object,
    },
  },
  data() {
    return {
      // 表单提交
      submitForm: {
        name: "",
        sort: 0,
      },
      // 表单验证
      submitFormRules: {
        name: [{ required: true, message: "请输入标题", trigger: "blur" }],
        sort: [{ required: true, message: "请输入权重", trigger: "blur" }],
      },
      // 数据
      dialogData: {
        title: "",
      },
      // 加载状态
      isLoading: {
        submit: false,
      },
      // 是否弹窗
      isDialog: true,
    };
  },
  created() {
    if (this.info) {
      this.dialogData.title = "修改" + "(" + this.info.name + ")";
      this.getData();
    } else {
      this.dialogData.title = "添加";
    }
  },
  methods: {
    getData() {
      let dataInfo = this.info;
      this.submitForm = {
        cate_id: dataInfo.cate_id,
        name: dataInfo.name,
        sort: Number(dataInfo.sort),
      };
    },
    // 提交
    handleSubmit() {
      this.$refs.submitFormRef.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.isLoading.submit = true;

        if (this.info) {
          edit(this.submitForm)
            .then(() => {
              this.isLoading.submit = false;
              this.$emit("close");
            })
            .catch(() => {
              this.isLoading.submit = false;
            });
        } else {
          add(this.submitForm)
            .then(() => {
              this.isLoading.submit = false;
              this.$emit("close");
            })
            .catch(() => {
              this.isLoading.submit = false;
            });
        }
      });
    },
    // 图片上传
    uploadImage(param) {
      const fileObj = param.file;
      const form = new FormData();
      form.append("file", fileObj);
      uploadFiles(form).then((res) => {
        this.submitForm.picture_path = res.data.fileurl;
      });
    },
    // 关闭
    handleClose() {
      this.$emit("close", "cancel");
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-textarea textarea {
  font-family: "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

::v-deep .tox-tinymce {
  min-height: 500px;
}

.image-box {
  width: 100%;
  height: 100%;
}
</style>
